package com.ks.app.product.service.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ks.app.admin.service.impl.BaseServiceImpl;
import com.ks.app.admin.util.Pager;
import com.ks.app.logger.Log4jUtil;
import com.ks.app.product.hibernate.KRoom;
import com.ks.app.product.hibernate.KRoomImage;
import com.ks.app.product.service.KsroomService;

@Service("ksroomServiceimpl")
public class KsroomServiceImpl extends BaseServiceImpl implements KsroomService {
	
	private String className = "KsroomServiceImpl.";
	private Logger logger = Log4jUtil.getBusinessLogger();
	
	public List<KRoom> GetKRoomList(String roommerId,String roomNo,String sd,
			String ed, String roomisshow, Pager pager,String roomType) {

		String method = "GetRoomList()";
		String sql = " from KRoom room where 1=1 ";
		
		if (roommerId != null && !"".equals(roommerId)) {
			sql = sql + "  and room.roomID=? ";
		}
		if (roomNo != null && !"".equals(roomNo)) {
			sql = sql + "  and room.roomNo=? ";
		}
		if (roomType != null && !"".equals(roomType)) {
			sql = sql + "  and room.roomType=? ";
		}
		if (sd != null && !"".equals(sd)) {
			sql = sql + "  and room.roomStartDate=? ";
		}
		if (ed != null && !"".equals(ed)) {
			sql = sql + "  and room.roomEndDate=? ";
		}
		if (roomisshow != null && !"".equals(roomisshow)) {
			sql = sql + "  and room.roomState=? ";
		}
		Query query = em.createQuery(sql + " order by roomNo,room.merRoom.ksMerRegistId ");
		int i = 0;
		if (roommerId != null && !"".equals(roommerId)) {
			i++;
			long mid = Long.parseLong(roommerId);
			query.setParameter(i, mid);
		}
		if (roomNo != null && !"".equals(roomNo)) {
			i++;
			query.setParameter(i, roomNo);
		}
		if (roomType != null && !"".equals(roomType)) {
			i++;
			query.setParameter(i, roomType);
		}
		if (sd != null && !"".equals(sd)) {
			i++;
			query.setParameter(i, sd);
		}
		if (ed != null && !"".equals(ed)) {
			i++;
			query.setParameter(i, ed);
		}
		if (roomisshow != null && !"".equals(roomisshow)) {
			i++;
			query.setParameter(i, roomisshow);
		}
		logger.info(className + method + "--------1 sql="+sql+"-------");
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		pager.setTotoalCnt(GetcntKRoom(roommerId,roomNo,sd, ed, roomisshow,roomType));
		@SuppressWarnings("unchecked")
		List<KRoom> list = query.getResultList();
		logger.info(className + method + "--get the length of list = " + list.size());
		
		return list;
	}

	// 获得记录行数
	public int GetcntKRoom(String roommerId,String roomNo, String sd, String ed,
			String roomState,String roomType) {
		
		String method = "GetcntKRoom()";

		String sql = " select count(*) from KRoom room where 1=1 ";
		if (roommerId != null && !"".equals(roommerId)) {
			sql = sql + "  and room.roomID=? ";
		}
		if (roomNo != null && !"".equals(roomNo)) {
			sql = sql + "  and room.roomNo=? ";
		}
		if (roomType != null && !"".equals(roomType)) {
			sql = sql + "  and room.roomType=? ";
		}
		if (sd != null && !"".equals(sd)) {
			sql = sql + "  and room.roomStartDate=? ";
		}
		if (ed != null && !"".equals(ed)) {
			sql = sql + "  and room.roomEndDate=? ";
		}
		if (roomState != null && !"".equals(roomState)) {
			sql = sql + "  and room.roomState=? ";
		}
		Query query = em.createQuery(sql);
		int i = 0;
		if (roommerId != null && !"".equals(roommerId)) {
			i++;
			long mid = Long.parseLong(roommerId);
			query.setParameter(i, mid);
		}
		if (roomNo != null &&!"".equals(roomNo)) {
			i++;
			query.setParameter(i, roomNo);
		}
		if (roomType != null && !"".equals(roomType)) {
			i++;
			query.setParameter(i, roomType);
		}
		if (sd != null && !"".equals(sd)) {
			i++;
			query.setParameter(i, sd);
		}
		if (ed != null && !"".equals(ed)) {
			i++;
			query.setParameter(i, ed);
		}
		if (roomState != null && !"".equals(roomState)) {
			i++;
			query.setParameter(i, roomState);
		}
		logger.info(className + method + "----show the sql --"+sql);
		return Integer.parseInt(String.valueOf(query.getSingleResult()));
	}

	@Override
	public int addKRoom(KRoom room) {
		int tag = 0;
		System.out.println("come in for addkroom -----------");
		if (room != null) {
			em.merge(room);
			tag = 1;
			System.out.println(tag);
			return tag;
		} else {
			System.out.println(tag);
			return tag;
		}
		
	}

	@SuppressWarnings("unchecked")
	public List<KRoom> GetKRoom(String uiid) {
		String sql = "from KRoom where 1=1";
		if(uiid !=null && !"".equals(uiid)){
			long rid = Long.parseLong(uiid);
			sql = sql+" and roomID ='"+rid+"'";
		}
		Query query = em.createQuery(sql);
		List<KRoom> list = query.getResultList();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<KRoom> GetKRoom() {
		String sql = "from KRoom";
		Query query = em.createQuery(sql);
		List<KRoom> list = query.getResultList();
		return list;
	}

	@Override
	public int insertPhoto(KRoomImage roomImage) {
		if(roomImage != null){
			em.merge(roomImage);
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteKRoom(String roomid) {
		
		String sql = "from KRoom where 1=1";
		String method = "deleteKRoom";
		
		if(roomid!=null && !"".equals(roomid)){
			long rid = Long.parseLong(roomid);
			sql = sql+" and roomID =" + rid;
			logger.info(className + "the sql centence is " + sql);
			Query query = em.createQuery(sql);
			//需要取得商家再由商家取得房间集合，然后从集合中剔除该房间
			KRoom room = (KRoom) query.getResultList().get(0);
			logger.info(className + method + "-------------------- " + room.getRoomType());
			room.getMerRoom().getMerRoom().remove(room);
			em.remove(room);
			logger.info(className + "-------------------- " + sql);
			return 1;
		}else{
			return 0;
		}
	}
}
